<template>
  <div id="goods" class="goods">
    <div class="menu-wrapper" ref="menuWrapper">
      <ul class="menu">
        <li @click="selectMenu(index, $event)" v-for="(item, index) in goods" :key="index" class="menu-item" :class="{'current':currentIndex===index}">
          <span class="text border-1px">
            <v-icon v-if="item.type>0" class="icon" :type="item.type" size="12px" :extra="3"></v-icon>{{item.name}}</span>
        </li>
      </ul>
    </div>
    <div class="foods-wrapper" ref="foodsWrapper">
      <ul>
        <li class="food-list" ref="foodList" v-for="(goodItem, index) in goods" :key="index">
          <h1 class="title">{{goodItem.name}}</h1>
          <ul>
            <li class="food-item border-1px" v-for="(foodItem, index) in goodItem.foods" :key="index" @click="selectFood(foodItem, $event)">
              <div class="icon">
                <img :src="foodItem.icon" width="57" height="57">
              </div>
              <div class="content">
                <h2 class="name">{{foodItem.name}}</h2>
                <p v-if="foodItem.description" class="desc">{{foodItem.description}}</p>
                <div class="extra">
                  <span class="count">月售{{foodItem.sellCount}}份</span>
                  <span>好评率{{foodItem.rating}}%</span>
                </div>
                <div class="price">
                  <span class="now">￥{{foodItem.price}}</span>
                  <span v-if="foodItem.oldPrice" class="old">￥{{foodItem.oldPrice}}</span>
                </div>
                <div class="cartcontrol-wrapper">
                  <cart-control @add="addFood" :food="foodItem"></cart-control>
                </div>
              </div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <shop-cart ref="shopcart" :deliveryPrice="seller.deliveryPrice" :minPrice="seller.minPrice" :selectedFoods="selectedFoods"></shop-cart>
    <food @add="addFood" :food="selectedFood" :visible.sync="foodShow"></food>
  </div>
</template>

<script>
import vIcon from '../icon/icon';
import BScroll from 'better-scroll';
import ShopCart from '../shopcart/shopcart';
import CartControl from '../cartcontrol/cartcontrol';
import Food from '../food/food';

let ERR_OK = 0;

export default {
  name: 'goods',
  props: {
    seller: {
      type: Object
    }
  },
  data() {
    return {
      goods: [],
      listHeight: [],
      scrollY: 0,
      foodShow: false,
      selectedFood: {}
    };
  },
  computed: {
    currentIndex() {
      for (let i = 0; i < this.listHeight.length; i++) {
        let height1 = this.listHeight[i];
        let height2 = this.listHeight[i + 1];
        if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
          return i;
        }
      }
      return 0;
    },
    selectedFoods() {
      let foods = [];

      this.goods.forEach(function (good) {
        good.foods.forEach(function (food) {
          if (food.count > 0) {
            foods.push(food);
          }
        }, this);
      }, this);

      return foods;
    }
  },
  methods: {
    addFood(target) {
      this._drop(target);
    },
    selectFood(food, event) {
      if (!event._constructed) {
        return;
      }
      this.selectedFood = food;
      this.foodShow = true;
    },
    _drop(target) {
      // 体验优化,异步执行下落动画
      this.$nextTick(() => {
        this.$refs.shopcart.drop(target);
      });
    },
    _initScroll() {
      let _this = this;
      _this.menuScroll = new BScroll(this.$refs.menuWrapper, { click: true });
      _this.foodsScroll = new BScroll(this.$refs.foodsWrapper, { click: true, probeType: 3 });

      this.foodsScroll.on('scroll', function (pos) {
        _this.scrollY = Math.abs(Math.round(pos.y));
      });
    },
    _calculateHeight() {
      let height = 0;
      let foodList = this.$refs.foodList;

      this.listHeight.push(height);
      for (let i = 0; i < foodList.length; i++) {
        let item = foodList[i];
        height += item.clientHeight;
        this.listHeight.push(height);
      }
    },
    selectMenu(index, event) {
      if (!event._constructed) {
        return;
      }

      let foodList = this.$refs.foodList;
      let el = foodList[index];
      this.foodsScroll.scrollToElement(el, 300);

    }
  },
  created() {
    this.$http.get('/api/goods').then((response) => {
      if (response.data.errno === ERR_OK) {
        this.goods = response.data.data;

        //Dom更新，初始化滚动插件
        this.$nextTick(() => {
          this._initScroll();
          this._calculateHeight();
        });
      }
    });
  },
  components: {
    vIcon,
    ShopCart,
    CartControl,
    Food
  }
}
</script>

<style lang="less" scoped>
@import "../../common/styles/mixin.less";

.goods {
  position: absolute;
  top: 174px;
  bottom: 46px;
  left: 0;
  width: 100%;
  overflow: hidden;
  display: flex;
  .menu-wrapper {
    flex: 0 0 80px;
    width: 80px;
    background: #f3f5f7;
    .menu {
      display: table-cell;
      .menu-item {
        display: table;
        height: 54px;
        padding: 0 12px;
        &.current {
          background: #fff;
        }
        .text {
          font-size: 12px;
          line-height: 14px;
          display: table-cell;
          vertical-align: middle;
          .icon {
            margin-right: 2px;
          }
          .border-1px(rgba(7, 17, 27, 0.1));
          width: 56px;
        }
      }
    }
  }
  .foods-wrapper {
    flex: 1;

    .food-list {
      .title {
        display: block;
        background: #f3f5f7;
        height: 26px;
        font-size: 12px;
        color: #93999f;
        line-height: 26px;
        padding-left: 14px;
        border-left: 2px solid #d9dde1;
      }

      .food-item {
        display: flex;
        margin: 18px;
        padding-bottom: 18px;
        font-size: 0;
        .border-1px(rgba(7, 17, 27, 0.1));
        &:last-child {
          .border-none;
          margin-bottom: 0;
        }
        .icon {
          margin-right: 10px;
          flex: 0 0 57px;
        }
        .content {
          flex: 1;
          .name {
            margin: 2px 0 8px 0;
            font-size: 14px;
            color: rgb(7, 17, 27);
            line-height: 14px;
          }
          .desc,
          .extra {
            font-size: 10px;
            color: rgb(147, 153, 159);
            line-height: 10px;
          }
          .desc {
            line-height: 12px;
            margin-bottom: 8px;
          }
          .extra {
            .count {
              margin-right: 12px;
            }
          }
          .price {
            line-height: 24px;
            font-weight: 700;
            .now {
              margin-right: 8px;
              font-size: 14px;
              color: rgb(240, 20, 20);
            }
            .old {
              text-decoration: line-through;
              font-size: 10px;
              color: rgb(147, 153, 159);
            }
          }
          .cartcontrol-wrapper {
            position: absolute;
            bottom: 12px;
            right: 0;
          }
        }
      }
    }
  }
}
</style>